package com.itheima.easy.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.easy.constant.TradeConstant;
import com.itheima.easy.handler.FaceToFacePayHandler;
import com.itheima.easy.vo.TradeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName FaceToFacePayController.java
 * @Description 面对面支付
 */
@RequestMapping("trade-face-to-face-feign")
@RestController
@Api(tags = "面对面支付")
public class FaceToFacePayController {

    @Autowired
    FaceToFacePayHandler aliFaceToFacePayHandler;

    @Autowired
    FaceToFacePayHandler wechatFaceToFacePayHandler;

    /***
     * @description 扫码-用户扫商家，完成付款
     * @param tradeVo 订单
     * @return  支付结果
     */
    @PostMapping("pay")
    @ApiOperation(value = "扫码-商家扫用户",notes = "扫码-商家扫用户")
    @ApiImplicitParam(name = "tradeVo",value = "交易单",required = true,dataType = "TradeVo")
    @ApiOperationSupport(includeParameters ={"tradeVo.tradeOrderNo","tradeVo.memo",
            "tradeVo.authCode","tradeVo.tradeAmount","tradeVo.tradeChannel"})
    TradeVo payTrade(@RequestBody TradeVo tradeVo){
        TradeVo tradeVoResult = null;
        if (TradeConstant.TRADING_CHANNEL_ALI_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= aliFaceToFacePayHandler.payTrade(tradeVo);
        }else if (TradeConstant.TRADING_CHANNEL_WECHAT_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= wechatFaceToFacePayHandler.payTrade(tradeVo);
        }else {
            throw new RuntimeException("请输入渠道！");
        }
        return tradeVoResult;
    }

    /***
     * @description 生成交易付款码，待用户扫码付款
     * @param tradeVo 订单
     * @return  二维码路径
     */
    @PostMapping("precreate")
    @ApiOperation(value = "扫码-用户扫商家",notes = "扫码-用户扫商家")
    @ApiImplicitParam(name = "tradeVo",value = "交易单",required = true,dataType = "TradeVo")
    @ApiOperationSupport(includeParameters ={"tradeVo.tradeOrderNo","tradeVo.memo",
            "tradeVo.tradeAmount","tradeVo.tradeChannel"})
    TradeVo precreateTrade(@RequestBody TradeVo tradeVo){
        TradeVo tradeVoResult = null;
        if (TradeConstant.TRADING_CHANNEL_ALI_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= aliFaceToFacePayHandler.precreateTrade(tradeVo);
        }else if (TradeConstant.TRADING_CHANNEL_WECHAT_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= wechatFaceToFacePayHandler.precreateTrade(tradeVo);
        }else {
            throw new RuntimeException("请输入渠道！");
        }
        return tradeVoResult;
    }

}
